from typing import Union, List, Optional
from fastapi import FastAPI, Form, File, UploadFile, Request
from pydantic import BaseModel, Field, ValidationError, validator, validators
import uvicorn
from datetime import date
import sys
import os

app = FastAPI()

@app.post("/items")
async def items(request: Request):
    # 适合小文件上传
    print("URL: ", request.url)
    print("客户端IP地址: ", request.client.host)
    print("客户端的端口: ", request.client.port)
    print("客户端的user-agent: ", request.headers.get("user-agent"))

    return {
        "url": request.url,
        "client_host": request.client.host,
        "client_port": request.client.port,
        "user-agent": request.headers.get("user-agent"),
        "cookies": request.cookies
    }


if __name__ == '__main__':
    print(sys.argv)

    # 获取当前文件的完整路径
    file_path = __file__
    # 获取文件名
    file_name = os.path.basename(file_path)
    print(file_name)

    start_args = file_name.split(".py")[0] + ":app"

    uvicorn.run(start_args, host="127.0.0.1", port=8000, reload=True)